var currentRandomIdx;
var questionTimer;
var i18nAnswer = [
	"Bien joué !",
	"C'est ça !",
	"Bravo !"
];

/**
 * Display new question
 */
function displayNextQuestion() {
	showContainer('trainingContainer');

	$('#answerInput').val('');
	$('#answerInput').focus();
	if (!!questionTimer) clearTimeout(questionTimer);
	if (selectedWordsArray.length > 0) {
		numberTries++;
		
		currentRandomIdx = Math.floor(Math.random() * selectedWordsArray.length);
		var word = selectedWordsArray[currentRandomIdx];
		var questionDir = buildPathForQuestion(selectedBatterie.id, word.id);
		$('.tip.pageTip .value').html(pageI18NArray[word.page]);/* TODO: I18N */
		// image or text
		if (word.hasImage == 1) {
			$('.tip.questionTip .value').addClass('hidden');
			$('.tip.questionTip .img').removeClass('hidden');
			/* ¤ONLINE */
			$('.tip.questionTip .img').attr('src', questionDir + word.image);
			/* ¤ONLINEEND */
			/* ¤OFFLINE
			$('.tip.questionTip .img').attr('src', word.image);
			¤OFFLINEEND */
		}else{
			$('.tip.questionTip .img').addClass('hidden');
			$('.tip.questionTip .value').removeClass('hidden');
			if (isInverse) {
				$('.tip.questionTip .value').html(getRightAnswer());
			}else{
				$('.tip.questionTip .value').html(word.question);
			}
		}
		// audio
		if (word.hasSound == 1) {
			$('.tip.questionTip .audio').removeClass('hidden');
			$('.tip.questionTip .audio source').remove();
			/* ¤ONLINE */
			$('.tip.questionTip .audio').append($('<source>').attr('src', questionDir + word.sound).attr('type', 'audio/mp3'));
			/* ¤ONLINEEND */
			/* ¤OFFLINE
			$('.tip.questionTip .audio').append($('<source>').attr('src', word.sound).attr('type', 'audio/mp3'));
			¤OFFLINEEND */
			setTimeout(function() {
				if (!!$('.tip.questionTip .audio') && !!$('.tip.questionTip .audio')[0] && $('.tip.questionTip .audio')[0].play) {
					$('.tip.questionTip .audio')[0].play();
				}
			}, 1000);
		}else{
			$('.tip.questionTip .audio').addClass('hidden');
		}
		
		// number
		$('.tip.wordTip .value').html(word.numero);
		
		// manage tempo
		if (hasTempo) {
			questionTimer = setTimeout(function(){
				$('.tip.questionTip .value').html('');
			}, tempo * 1000);
		}
		
		// set focus
		$('#answerInput').focus();
	}else{
		// finish !
		var secondesSpent = Math.floor(((new Date()).getTime() - startTime.getTime()) / 1000);
		if (user.default_assist == "1") {
			var resultPrefix = (numberQuestions >= numberTries) ? 'right' : 'wrong';
			displayWidePopup ("", "<div class='assistedFinalBilan " + resultPrefix + "Result'><div><span class='rightAnswerFeedback'>Super !</span></div><div><span class='littleRightIcon'></span><span class='" + resultPrefix + "Icon'></span><span class='littleRightIcon'></span></div><div class='bilan_number_anwser'>" + numberQuestions + " réponses</div><div class='bilan_number_tries'>" + numberTries + " essais</div><div class='bilan_number_secondes'>" + secondesSpent + " secondes</div></div>", backFromWords, 12, 'assitedBilan');
		} else {
			displayPopup ("Bravo, vous avez terminé en " + secondesSpent + " secondes", "Bilan", backFromWords);
		}
	}
}

function getRightAnswer() {
	return (isDictee) ? selectedWordsArray[currentRandomIdx].reponse : selectedWordsArray[currentRandomIdx].numero;
}

/**
 * Answer the question
 */
function answer() {
	hideContainers();
	var rightAnswer;
	if (isInverse) {
		rightAnswer = selectedWordsArray[currentRandomIdx].question;
	}else{
		rightAnswer = getRightAnswer();
	}
	if ($('#answerInput').val() == rightAnswer) {
		var newArray = new Array();
		// Remove this word from the array
		for (var i=0, j=0;i<selectedWordsArray.length;i++) {
			if (i != currentRandomIdx) {
				newArray[j++] = selectedWordsArray[i];
			}
		}
		selectedWordsArray = newArray;
		if (user.default_feedback == "1") {
			if (user.default_assist == "1") {
				displayWidePopup("", "<div class='greenFire'></div><span class='rightAnswerFeedback'>Bonne réponse</span>", displayNextQuestion, 2);
			} else {
				displayPopup(i18nAnswer[Math.floor(Math.random() * 3)], "<span class='rightAnswerFeedback'>Bonne réponse</span>", displayNextQuestion);
			}
		}else{
			displayNextQuestion();
		}
	}else{
		if (user.default_assist == "1") {
			displayWidePopup("", "<div class='redFire'></div><span class='wrongAnswerFeedback'>" + rightAnswer + "</span>", displayNextQuestion, 2);
		} else {
			displayPopup("Dommage, la bonne réponse était \"<span class='wrongAnswerFeedback'>" + rightAnswer + '</span>"', "Erreur", displayNextQuestion);
		}
	}
}
